package extra.navigator;

import java.io.*;
import java.util.*;

public class ExtraNavigatorData {
	List<FileData> Files = new ArrayList<FileData>();
	List<TagData> Tags = new ArrayList<TagData>();
	int maxfileid;
	private static String fileIndexFileName = "1.index";
	private static String tagIndexFileName = "2.index";

	public ExtraNavigatorData() {
		// Get Infomations From DataFile
		Tags = new LinkedList<TagData>();
		Files = new LinkedList<FileData>();
		
		TagData xxx = new TagData();
		xxx.TagName = "First";
		xxx.Number = 2;
		Tags.add(xxx);
		xxx = new TagData();
		xxx.TagName = "Second";
		xxx.Number = 1;
		Tags.add(xxx);
		xxx = new TagData();
		xxx.TagName = "Third";
		xxx.Number = 6;
		Tags.add(xxx);
		xxx = new TagData();
		xxx.TagName = "4";
		xxx.Number = 5;
		Tags.add(xxx);
		xxx = new TagData();
		xxx.TagName = "Firefox";
		xxx.Number = 2;
		Tags.add(xxx);
		xxx = new TagData();
		xxx.TagName = "Sun JAVA";
		xxx.Number = 1;
		Tags.add(xxx);
		xxx = new TagData();
		xxx.TagName = "Extra";
		xxx.Number = 6;
		Tags.add(xxx);
		xxx = new TagData();
		xxx.TagName = "SO FUNCKING";
		xxx.Number = 5;
		Tags.add(xxx);
		xxx = new TagData();
		xxx.TagName = "xxx";
		xxx.Number = 2;
		Tags.add(xxx);
		xxx = new TagData();
		xxx.TagName = "return";
		xxx.Number = 1;
		Tags.add(xxx);
		xxx = new TagData();
		xxx.TagName = "...";
		xxx.Number = 6;
		Tags.add(xxx);
		xxx = new TagData();
		xxx.TagName = "Open Office.org";
		xxx.Number = 5;
		Tags.add(xxx);
		FileData xx = new FileData();
		xx.FileName = "java.exe";
		xx.Position = "java.exe";
		xx.FileType = 1;
		xx.Visited = 4;
		Files.add(xx);

		xx = new FileData();
		xx.FileName = "FileFox";
		xx.Position = "C:\\\"Program Files\"\\\"Mozilla Firefox\"\\FireFox.exe";
		xx.FileType = 1;
		xx.Visited = 2;
		Files.add(xx);

		xx = new FileData();
		xx.FileName = "C:\\";
		xx.FileType = 0;
		xx.Position = "C:\\";
		xx.Visited = 3;
		Files.add(xx);

		xx = new FileData();
		xx.FileName = "logo.png";
		xx.FileType = 3;
		xx.Position = "C:\\logo.png";
		Files.add(xx);

		save();
		 read();
		// this.printOutFileData(this.search("a"));
		
	}

	public void save() {
		// Save Infomation To File
		try {
			ObjectOutputStream out1 = new ObjectOutputStream(
					new FileOutputStream(this.fileIndexFileName));
			out1.writeObject(this.Files);
			out1.close();

			ObjectOutputStream out2 = new ObjectOutputStream(
					new FileOutputStream(this.tagIndexFileName));
			out2.writeObject(this.Tags);
			out2.close();

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void read() {
		// read from
		try {
			ObjectInputStream in1 = new ObjectInputStream(new FileInputStream(
					this.fileIndexFileName));
			this.Files = (List<FileData>) in1.readObject();
			in1.close();

			ObjectInputStream in2 = new ObjectInputStream(new FileInputStream(
					this.tagIndexFileName));
			this.Tags = (List<TagData>) in2.readObject();
			in2.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void add(String path) {
		add(path, false, false, "");
	}

	public void add(String path, boolean hidden, boolean secret, String tag1) {
		// ����FileData
		FileData newfile = new FileData();
		File toadd = new File(path);
		if ( !toadd.exists() ) {
			System.out.println("Faild to open File");
			return;
		}
		String filename = toadd.getName();
		newfile.FileName = filename;
		newfile.Position = path;
		newfile.FileType = getFileType(toadd);
		newfile.Position = toadd.getAbsolutePath();
		// newfile.Group
		// newfile.LastVisited = toadd.lastModified();
		newfile.Visited = 0;
		newfile.Hidden = hidden;
		newfile.Secret = secret;
		newfile.Tag1 = tag1;
		// ���뵽Files
		this.Files.add(newfile);
		// ���� TagData
		TagData tag = new TagData();
		// tag.Number =
		// tag.TagName=tag1;
		// ���뵽Tags
		this.Tags.add(tag);

		// �����
		// save();
	}

	public void delete(String path) {
		for (FileData fd : this.Files) {
			if (fd.Position == path) {
				this.Files.remove(fd);
				break;
			}
		}
	}

	@SuppressWarnings("unchecked")
	public List<FileData> search(String q) {
		// Ѱ���ַ�
		List<FileData> result = new ArrayList<FileData>();
		for (FileData fd : this.Files) {
			if (fd.FileName.contains(q)) {
				result.add(fd);
				continue;
			}
		}
		// �Խ������
		FileDataComparator comp = new FileDataComparator();

		Collections.sort(this.Files, comp);

		return result;
	}

	public void visited(FileData fd) {
		int index = this.Files.indexOf(fd);
		this.Files.get(index).Visited++;
	}

	private void printOutFileData(List<FileData> list) {
		for (FileData fd : list) {
			System.out.println(fd.toString());
		}

		System.out.println("total size:" + list.size());
	}

	private int getFileType(File file) {
		if (file.isDirectory())
			return 0;
		String ext = getFileExtension(file);
		if (ext != null) {
			if (ext.equals("exe"))
				return 1;
			if (ext.equals("txt"))
				return 2;
			if (ext.equals("wav"))
				return 3;
			if (ext.equals("mp3"))
				return 3;
			// bmp/png/jpeg/gif
			if (ext.equals("bmp"))
				return 4;
			if (ext.equals("png"))
				return 4;
			if (ext.equals("jpeg"))
				return 4;
			if (ext.equals("gif"))
				return 4;

			if (ext.equals("html"))
				return 5;
			if (ext.equals("htm"))
				return 5;
		}
		return 9;
	}

	/**
	 * ��ȡָ���ļ���)չ��
	 * 
	 * @param f
	 * @return
	 */
	private String getFileExtension(File f) {
		if (f != null) {
			String filename = f.getName();
			int i = filename.lastIndexOf('.');
			if (i > 0 && i < filename.length() - 1) {
				return filename.substring(i + 1).toLowerCase();
			}
		}
		return null;
	}

}

class FileData implements Serializable {
	String FileName;
	/*
	 * type 0 dir 1 exe 2 txt 3 wav/mp3 4 bmp/png/jpeg/gif 5 html 9 Others
	 */
	Integer FileType = 0;
	String Position = "";
	Integer Group = 0;
	Date LastVisited = new Date();
	Integer Visited = 0;
	Boolean Hidden = false;
	Boolean Secret = false;
	String Tag1 = "";
	String Tag2 = "";
	String Tag3 = "";
	String Tag4 = "";
	String Tag5 = "";

	public String toString() {
		return this.FileName + "\tVisited:" + this.Visited;
	}
}

class TagData implements Serializable {
	String TagName = "";
	Integer Number = 0;
}

class GroupData {
	String GroupName;
}

class IconData {
	String GroupName;
	Integer GroupID;
}

class FileDataComparator implements Comparator {

	public int compare(Object o1, Object o2) {
		// TODO Auto-generated method stub
		FileData c1 = (FileData) o1;
		FileData c2 = (FileData) o2;
		if (c1.Visited <= c2.Visited) {
			return 1;
		} else {
			return -1;
		}
	}
}